package org.tinygroup.cepcoreimpl.test.newcase.testcase;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.tinygroup.cepcore.CEPCore;
import org.tinygroup.cepcore.EventProcessor;
import org.tinygroup.cepcoreimpl.CEPCoreImpl;
import org.tinygroup.cepcoreimpl.test.newcase.VirtualEventProcesor;
import org.tinygroup.cepcoreimpl.test.newcase.VirtualService;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.event.ServiceInfo;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;

public abstract class BaseNewCase extends TestCase{
	protected static Logger LOGGER = LoggerFactory.getLogger(BaseNewCase.class);
	
	protected ServiceInfo getRemoteMethod(CEPCore cepcore, String serviceId) {
		try {
			Method method = CEPCoreImpl.class.getDeclaredMethod("getRemoteServiceInfo", String.class);
			method.setAccessible(true);
			return (ServiceInfo) method.invoke(cepcore, new Object[] {serviceId});
		} catch (Exception e) {
			return null;
		}
	}
	protected List<ServiceInfo> getService(String ids) {
		List<ServiceInfo> list = new ArrayList<ServiceInfo>();
		String[] service = ids.split(",");
		for (String id : service) {
			VirtualService v = new VirtualService(id);
			list.add(v);
		}
		return list;
	}

	private EventProcessor getEventProcessor(String id, int type, String ids) {
		VirtualEventProcesor eventProcessor = new VirtualEventProcesor();
		eventProcessor.setId(id);
		eventProcessor.getServiceInfos().addAll(getService(ids));
		eventProcessor.setType(type);
		return eventProcessor;
	}
	
	private List<String> trans(String str){
		
		List<String> list = new ArrayList<String>();
		if(StringUtil.isBlank(str)){
			return list;
		}
		String[] array = str.split(",");
		for(String s : array){
			list.add(s);
		}
		return list;
	}

	/**
	 * 
	 * @description：
	 * @localServiceIds：已经注册的本地服务id集
	 * @unRegLocalServiceIds:已经注销的本地服务id集
	 * @remoteServiceIds：已经注册的远程服务id集
	 * @unregRemoteServiceIds：已经注销的远程服务id集
	 * @eventProcessors：处理器集
	 * @author: qiucn
	 * @version: 2016年8月1日上午9:24:08
	 */
	protected void assertCepcoreInfo(CEPCore cepcore, String localServiceIds,String unRegLocalServiceIds,
			String remoteServiceIds, String unRegRemoteServiceIds, String eventProcessors) {
		LOGGER.logMessage(LogLevel.INFO, "localServiceIds:{}",localServiceIds);
		LOGGER.logMessage(LogLevel.INFO, "remoteServiceIds:{}",remoteServiceIds);
		LOGGER.logMessage(LogLevel.INFO, "eventProcessors:{}",eventProcessors);
		List<String> localServiceIdsList = trans(localServiceIds);
		List<String> unRegLocalServiceIdsList = trans(unRegLocalServiceIds);
		List<String> remoteServiceIdsList = trans(remoteServiceIds);
		List<String> unregRemoteServiceIdsList = trans(unRegRemoteServiceIds);
		List<String> eventProcessorsList = trans(eventProcessors);
		//本地服务列表
		List<ServiceInfo> services = cepcore.getServiceInfos();
		
		//已经注册的本地服务校验
		for(String serviceId:localServiceIdsList){
			boolean contains = false;
			for(ServiceInfo info : services){
				if(serviceId.equals(info.getServiceId())){
					contains = true;
				}
			}
			if(!contains){
				assertTrue(false);
			}
		}
		//已经注销的本地服务校验
		for(String serviceId : unRegLocalServiceIdsList){
			boolean contains = true;
			for(ServiceInfo info : services){
				if(serviceId.equals(info.getServiceId())){
					contains = false;
				}
			}
			if(contains){
				assertTrue(false);
			}
		}
		
		//如果已经注册的远程服务获取不到，则说明注册失败
		for(String serviceId:remoteServiceIdsList){
			ServiceInfo info = getRemoteMethod(cepcore,serviceId);
			if(info==null){
				assertTrue(false);
			}
		}
		
		//如果已经注销的远程服务还能获取到，则说明注销失败
		for(String serviceId : unregRemoteServiceIdsList){
			ServiceInfo info = getRemoteMethod(cepcore,serviceId);
			if(info!=null){
				assertTrue(false);
			}
		}
		
		List<EventProcessor> list = cepcore.getEventProcessors();
		assertEquals(eventProcessorsList.size(), list.size());
		
		for(EventProcessor processor:list){
			eventProcessorsList.remove(processor.getId());
		}
		assertEquals(0, eventProcessorsList.size());
		
		
		for(ServiceInfo service:services){
			localServiceIdsList.remove(service.getServiceId());
		}
		assertEquals(0, localServiceIdsList.size());
	}

	protected void registerEventProcessor(CEPCore cepcore,String id, int type, String ids) {
		EventProcessor processorLocal = getEventProcessor(id,type,ids);
		cepcore.registerEventProcessor(processorLocal);
	}
	
	protected void unRegisterEventProcessor(CEPCore cepcore, String id,
			int type, String ids) {
		EventProcessor processorRemote = getEventProcessor(id,type,ids);
		cepcore.unregisterEventProcessor(processorRemote);
	}
}
